from django.db import models

from . import settings


class Question(models.Model):
    text = models.CharField(max_length=settings.QUESTOIN_TEXT_MAX_LENGTH)
    publish_date = models.DateTimeField("date published")
    name = models.CharField(
        default="guest",
        max_length=settings.QUESTOIN_NAME_MAX_LENGTH,
    )

    def __str__(self):
        return self.text


class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    model_name = models.CharField(max_length=settings.MODEL_NAME_MAX_LENGTH)
    text = models.CharField(max_length=settings.ANSWER_TEXT_MAX_LENGTH)
    name = models.CharField(
        default="guest",
        max_length=settings.ANSWER_NAME_MAX_LENGTH,
    )
    # TODO: view count
    likes = models.IntegerField(default=0)
    dislikes = models.IntegerField(default=0)
    publish_date = models.DateTimeField("date published")

    def __str__(self):
        return f"{self.model_name}: {self.text[:15]} votes: {self.likes}vs.{self.dislikes}"
